const { dbUri } = require('./config.default');
const MongoClient = require("mongodb").MongoClient;


// const
// const ObjectID = require("mongodb").ObjectId;
const excludeFields_all = {
    _id: 0,
    isDeleted: 0,
    createdAt: 0,
    createdBy: 0,
    updatedAt: 0,
    updatedBy: 0,
};

const excludeFields_pcData = { isDeleted: 0, createdAt: 0, updatedAt: 0 }; //去掉了创建人，用来匹配是否允许删除

const DBNAME = "test";

module.exports = {
    createUniqueIndex:async(c,o)=>{
        const client = new MongoClient(dbUri);
        try {
            await client.connect();
            const db = client.db(DBNAME);
            //查询c集合
            const collection = db.collection(c);
            //按查询条件o
            // let result = await collection.createIndex({ theaterId: 1 }, { unique: true });
            let result = await collection.createIndex(o, { unique: true });
            return result;
        } finally {
            await client.close();
        }
    },
    distinct: async (c, o, field) => {
        const client = new MongoClient(dbUri);
        try {
            await client.connect();
            const db = client.db(DBNAME);
            //查询c集合
            const collection = db.collection(c);
            //按查询条件o
            let result = await collection.distinct(field, o);
            return result;
        } finally {
            await client.close();
        }
    },
    insertOne: async (c, o) => {
        const client = new MongoClient(dbUri);
        try {
            await client.connect();
            const db = client.db(DBNAME);
            //查询c集合
            const collection = db.collection(c);
            //按查询条件o
            let result = await collection.insertOne(o);
            return result;
        } finally {
            await client.close();
        }
    },
    update: async (c, w, o, is_upsert = true) => {
        const client = new MongoClient(dbUri);
        try {
            await client.connect();
            const db = client.db(DBNAME);
            //查询c集合
            const collection = db.collection(c);
            //按查询条件w更新o，{upsert:true}:条件w没有就添加
            let result = null;
            if (is_upsert) {
                //没有就新增
                result = await collection.updateOne(w, o, { upsert: is_upsert });
            } else {
                //默认第三个参数默认 { upsert: false }
                result = await collection.updateOne(w, o);
            }

            return result;
        } finally {
            await client.close();
        }
    },
    updateMany: async (c, w, o, is_upsert = false) => {
        const client = new MongoClient(dbUri);
        try {
            await client.connect();
            const db = client.db(DBNAME);
            //查询c集合
            const collection = db.collection(c);
            //按查询条件w更新o，{upsert:true}:条件w没有就添加
            let result = null;
            if (is_upsert) {
                //没有就新增
                result = await collection.updateMany(w, o, { upsert: is_upsert });
            } else {
                //默认第三个参数默认 { upsert: false }
                result = await collection.updateMany(w, o);
            }

            return result;
        } finally {
            await client.close();
        }
    },
    insertMany: async (c, arr) => {
        const client = new MongoClient(dbUri);
        try {
            await client.connect();
            const db = client.db(DBNAME);
            const collection = db.collection(c);
            let result = await collection.insertMany(arr);
            return result;
        } finally {
            await client.close();
        }
    },
    deleteOne: async (c, w, o, isTrue = false) => {
        const client = new MongoClient(dbUri);
        try {
            await client.connect();
            const db = client.db(DBNAME);
            const collection = db.collection(c);
            let result = null;
            if (isTrue) {
                result = await collection.deleteOne(w);
            } else {
                o.isDeleted = true;
                result = await collection.updateOne(w, { $set: o });
            }
            return result;
        } finally {
            await client.close();
        }
    },
    deleteMany: async (c, w) => {
        const client = new MongoClient(dbUri);
        try {
            await client.connect();
            const db = client.db(DBNAME);
            const collection = db.collection(c);
            const result = await collection.deleteMany(w);
            return result;
        } finally {
            await client.close();
        }
    },
    deleteMany2: async (c, w, o, isTrue = false) => {
        const client = new MongoClient(dbUri);
        try {
            await client.connect();
            const db = client.db(DBNAME);
            const collection = db.collection(c);
            let result = null;
            if (isTrue) {
                result = await collection.deleteMany(w);
            } else {
                o.isDeleted = true;
                result = await collection.updateOne(w, { $set: o });
            }
            return result;
        } finally {
            await client.close();
        }
    },
    //collection、查询条件object cludefind的第二个参数过滤(select),譬如只返回_id == {_id:1}
    find: async (c, w, clude = null, is_limited = false, sortObj = false) => {
        const client = new MongoClient(dbUri);
        try {
            await client.connect();
            const db = client.db(DBNAME);
            const collection = db.collection(c);
            let result = null;

            if (!is_limited) {
                if (clude) {
                    if (sortObj) {
                        result = await collection.find(w).sort(sortObj).project(clude).toArray();
                    } else {
                        result = await collection.find(w).project(clude).toArray();
                    }

                } else {
                    if (sortObj) {
                        result = await collection.find(w).sort(sortObj).toArray();
                    } else {
                        result = await collection.find(w).toArray();
                    }

                }
            } else {
                if (sortObj) {
                    result = await collection
                        .find(w)
                        .sort(sortObj)
                        .project(excludeFields_all)
                        .limit(1)
                        .toArray();
                } else {
                    result = await collection
                        .find(w)
                        .project(excludeFields_all)
                        .limit(1)
                        .toArray();
                }


            }
            return result;
        } catch (e) {
            console.log(e);
        } finally {
            await client.close();
        }
    },

    findOrderAndPage: async (c, w, pageNo = 1, pageSize = 20, sortObjt = {}, isAll = false) => {
        const client = new MongoClient(dbUri);
        try {
            await client.connect();
            const db = client.db(DBNAME);
            const collection = db.collection(c);
            if (!isAll) {
                w.isDeleted = false;
            }
            let result = await collection
                .find(w)
                .project(excludeFields_pcData)
                .collation({ locale: "zh", numericOrdering: true })
                .sort(sortObjt)
                .skip((pageNo - 1) * pageSize)
                .limit(pageSize)
                .toArray();
            return result;
        } finally {
            await client.close();
        }
    },
    findCount: async (c, w) => {
        const client = new MongoClient(dbUri);
        try {
            await client.connect();
            const db = client.db(DBNAME);
            const collection = db.collection(c);
            let result = await collection.find(w).count();
            return result;
        } finally {
            await client.close();
        }
    },
    extendFunc: async (c, stages) => {
        const client = new MongoClient(dbUri);
        try {
            await client.connect();
            const db = client.db(DBNAME);
            const collection = db.collection(c);
            let result = await collection
                .aggregate(stages, {
                    collation: { locale: "zh", numericOrdering: true },
                })
                .toArray();
            // console.log(result)
            return result;
        } finally {
            await client.close();
        }
    },
};